import tempfile
import os
import commands

from ivic.vmc.Configure import *

class Nfs_Action:
	def __init__(self, config):
		self.local_path_prefix = config["nfsmount_root"]
	
	def parse_url(self, url,type=1):
		array = url.split("://");
		host_path_file = array[1];
		array = host_path_file.split("/",1)
		host = array[0];
		path_file = array[1];
		path_file = "/"+path_file;
		if type==2:
			return [host, path_file]

		file = os.path.basename(path_file);
		path = os.path.dirname(path_file);
		return [host.strip(), path.strip(),file.strip()]

	def convert_slash_to_underline(slef, path):
		array = path.split("/");
		return "_".join(array);

	def convert_underline_to_slash(self, path):
		array = path.split("_")
		return "/".join(array)

        def get_local_base_path(self, url):
                [nfsbase_hostname,nfsbase_path,nfsbase_file] = self.parse_url(url)
                _nfsbase_path = self.convert_slash_to_underline(nfsbase_path)
                local_base_path = self.local_path_prefix+"/"+nfsbase_hostname+"/"+_nfsbase_path
                return local_base_path


	def mount_nfs(self, nfsbase_url, nfscow_url,auuid,logger):
		[nfsbase_hostname,nfsbase_path, nfsbase_file] = self.parse_url(nfsbase_url)
		[nfscow_hostname,nfscow_path] = self.parse_url(nfscow_url,2)
		_nfsbase_path = self.convert_slash_to_underline(nfsbase_path)
		local_base_path = self.local_path_prefix + "/"+nfsbase_hostname + "/" +_nfsbase_path;
		remote_base_path = nfsbase_hostname+":"+nfsbase_path;
				
		_nfscow_path = self.convert_slash_to_underline(nfscow_path)
		local_cow_path = self.local_path_prefix + "/"+nfscow_hostname + "/" +_nfscow_path
		local_cow_path = local_cow_path.strip()
		remote_cow_path = nfscow_hostname + ":" + nfscow_path;
		remote_cow_path = remote_cow_path.strip()
		mount_nfsbase_command = "mount "+remote_base_path  + " "+local_base_path
		mount_nfscow_command = "mount "+remote_cow_path + " "+local_cow_path;
	        
		if not os.path.exists(local_base_path):
			status_output =  commands.getstatusoutput("mkdir -p  "+local_base_path)
			if status_output[0]!=0:
				raise Exception,"when create local base path#"+status_output[1]

		if not os.path.exists(local_cow_path):
			status_output = commands.getstatusoutput("mkdir -p "+ local_cow_path)
			if status_output[0]!=0:
				raise Exception,"when create cow path#"+status_output[1]

		if not os.path.ismount(local_base_path):
			status_output = commands.getstatusoutput(mount_nfsbase_command)
			if status_output[0]!=0:
				raise Exception,"when mount base dir#"+status_output[1]

		if not os.path.ismount(local_cow_path):
			status_output = commands.getstatusoutput(mount_nfscow_command);
			if status_output[0]!=0:
				raise Exception,"when mount cow dir#"+status_output[1]
                if auuid=="":
                   return local_cow_path;
                
		target_image = local_cow_path + "/"+auuid+".img"
		source_image = local_base_path+"/"+nfsbase_file
		if not os.path.exists(target_image):
			create_cow_image_command="qemu-img create -f qcow2 -b "+source_image+ " "+ target_image
			status_output = commands.getstatusoutput(create_cow_image_command)
			if status_output[0]!=0:
				raise Exception,"when qemu-img create@"+status_output[1]
				
		return target_image;

	def mount_all_nfs(self):
		if not os.path.exists(self.local_path_prefix):
			return;
		for dir in os.listdir(self.local_path_prefix):
			hostname = dir;
			for subdir in os.listdir(self.local_path_prefix+"/"+dir):
				_local_path = subdir;
				remote_path = self.convert_underline_to_slash(_local_path);
				if not os.path.ismount(self.local_path_prefix+"/"+hostname+"/"+_local_path):
					mount_command = "mount " + hostname+":"+remote_path+" "+self.local_path_prefix+"/"+hostname+"/"+_local_path
                                        commands.getstatusoutput(mount_command)
				#end if
			#end for
		#end for
				
if __name__ == "__main__":
	action = Nfs_Action()
	print action.parse_url("nfs://www.baidu.com/index")
	print action.convert_slash_to_underline("/a/b/c");
	print action.convert_underline_to_slash("_afads_afasdff")
	action.mount_nfs("nfs://219.224.171.159/var/lib/ivic/www/vstore/nfsbase/d928b63c-3a4b-11de-90cf-00237da1d732.img", "nfs://219.224.171.159/var/lib/ivic/www/vstore/nfscow","d928b63c-3a4b-11de-90cf-00237da1d732",None)
	action.mount_all_nfs();
	
